package com.baidu.fenfayun.demo.home
import android.content.*
import android.view.*
import android.widget.*
import com.baidu.yun.gameline.R
import android.graphics.drawable.*
import android.graphics.*

import org.jetbrains.anko.*
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.util.QMUIResHelper
import com.qmuiteam.qmui.util.QMUIViewHelper

import com.baidu.fenfayun.demo.model.QDItemDescription
import com.baidu.fenfayun.demo.auth.*
import com.baidu.fenfayun.demo.act.*
import com.baidu.fenfayun.demo.util.*
import com.qmuiteam.qmui.widget.grouplist.*
import com.qmuiteam.qmui.widget.dialog.*
import com.qmuiteam.qmui.widget.pullRefreshLayout.*

import net.steamcrafted.materialiconlib.*
import com.qmuiteam.qmui.alpha.QMUIAlphaImageButton

import kotlinx.android.synthetic.main.home_tweet_layout.view.*
class TweetController(context:Context): LayoutController(context,R.layout.home_tweet_layout) {



   var mQDItemDescription:QDItemDescription?  = null
   override fun getTitle():String  {
      return "动弹"
   }

   fun initOtherView(){
      val context = getContext()


   }  
   override fun initView() {
      super.initView()
      

      topbar.addRightImageButton(R.mipmap.icon_topbar_overflow, R.id.topbar_right_change_button).setOnClickListener( {
                showBottomSheetList();
            }
      )
      addRightImageButton(R.mipmap.icon_tabbar_lab_selected, QMUIViewHelper.generateViewId()).setOnClickListener( {
         Toast.makeText(getContext(), "开始搜索", Toast.LENGTH_SHORT).show()
         startFragment(SearchFragment())
      })

      initOtherView()
      initData()
   }

  fun showBottomSheetList() {
        val context = getContext()
        pull_to_refresh.setRefreshOffsetCalculator(QMUIFollowRefreshOffsetCalculator())
        //QMUICenterGravityRefreshOffsetCalculator,QMUIDefaultRefreshOffsetCalculator
        QMUIBottomSheet.BottomListSheetBuilder(context)
                .addItem("菜单A")
                .addItem("菜单B")
                .addItem("菜单D")
                .setOnSheetItemClickListener(object:QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener {
                    public override fun onClick(dialog:QMUIBottomSheet , itemView:View , position:Int, tag:String ) {
                        dialog.dismiss()
                        when(position){
                           0 -> context.toast("clickA") 
                           1 -> context.toast("clickB") 
                           2 -> context.toast("clickD") 
                        }

                     }
                  })
                .build()
                .show()
   }
   fun initData() {
        val datas  = listOf("人之初","性本善","性相近","习相远","苟不教","性乃迁","教之道","贵以专","昔孟母","择邻处","子不学","断机杼","窦燕山","有义方","教五子","名俱扬","养不教","父之过","教不严","师之惰","子不学","非所宜","幼不学","老何为","玉不琢","不成器","人不学","不知义","为人子","方少时","亲师友","习礼仪","香九龄","能温席","孝于亲","所当执","融四岁","能让梨","弟于长","宜先知","首孝弟","次见闻","知某数","识某文","一而十","十而百","百而千","千而万")
        listview.setAdapter(ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, datas));
        pull_to_refresh.setOnPullListener(object:QMUIPullRefreshLayout.OnPullListener {
            public override fun onMoveTarget(offset:Int) {

            }

            public override fun onMoveRefreshView(offset:Int) {

            }

            public override fun onRefresh() {
                pull_to_refresh.postDelayed(object:Runnable {
                    public override fun run() {
                        pull_to_refresh.finishRefresh()
                    }
                }, 2000)
            }
        })
    }
   fun addRightImageButton(drawableResId:Int, viewId:Int):QMUIAlphaImageButton  {
      val rightButton = IconUtil.generateImageButton(drawableResId,getContext())
      rightButton.setImageDrawable(IconUtil.getImage(getContext(),MaterialDrawableBuilder.IconValue.MAGNIFY))
      topbar.addRightView(rightButton, viewId, topbar.generateTopBarImageButtonLayoutParams())
      return rightButton
   }
}
